package com.xmx.ordercenter.controller;

import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import com.xmx.ordercenter.entity.CartEntity;
import com.xmx.ordercenter.service.CartService;
import com.xmx.common.utils.PageUtils;
import com.xmx.common.utils.R;
import io.swagger.annotations.Api;


/**
 * 购物车商品表
 *
 * @author fbb
 * @email boyinbing@126.com
 * @date 2021-08-11 01:30:41
 */
@Api(tags = "CartController", description = "购物车商品表服务")
@RestController
@RequestMapping("ordercenter/cart")
public class CartController {
    @Autowired
    private CartService cartService;



    /**
     * 列表
     */
    @GetMapping("/list")
    //@RequiresPermissions("ordercenter:cart:list")
    @ApiOperation("获取所有购物车商品表信息")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cartService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("ordercenter:cart:info")
    @ApiOperation("获取指定id的购物车商品表详情")
    public R info(@PathVariable("id") Integer id){
		CartEntity cart = cartService.getById(id);

        return R.ok().put("cart", cart);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("添加购物车商品表信息")
    //@RequiresPermissions("ordercenter:cart:save")
    public R save(@RequestBody Map<String, Object> params ){
        CartEntity cart = BeanUtil.mapToBean(params,CartEntity.class,false);
		cartService.save(cart);

        return R.ok().put("data",cart.getId());
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    @ApiOperation("更新指定id购物车商品表信息")
    //@RequiresPermissions("ordercenter:cart:update")
    public R update(@RequestBody CartEntity cart){
		cartService.updateById(cart);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除指定id购物车商品表信息")
    //@RequiresPermissions("ordercenter:cart:delete")
    public R delete(@RequestBody Integer[] ids){
		cartService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }



    @GetMapping("/count/{user_id}")
    public R count(@PathVariable("user_id")Integer uid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",uid);
        int count = cartService.count(queryWrapper);
        return R.ok().put("goodscount",count);
    }


    @GetMapping("/queryExit")
    public R fastadd(@RequestParam Integer goodsId ,@RequestParam Integer productId,@RequestParam Integer userId){
        CartEntity cartEntity = cartService.queryExist(goodsId, productId, userId);
        return R.ok().put("data",cartEntity);
    }

    @PutMapping("/clear")
    public R clearCart(@RequestBody Integer userId){
        QueryWrapper<CartEntity> queryWrapper =new QueryWrapper();
        queryWrapper.eq("user_id",userId);
        cartService.remove(queryWrapper);
        return R.ok();
    }


}
